from inc import * 
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,login,logout
from django.db import IntegrityError
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from hashlib import sha1
import cmd

#Build permission cmd_error
class Permission:
  @staticmethod
  def CreateService(request,sdata,cmd_error):
    if request.user.is_authenticated():
      sdata.email = request.user.email
    else:
      cmd_error['PERMISSION'] = "NEED LOGIN"

def GetUsr(request):
  if request.user.is_authenticated():
    return request.user.username
  else:
    return "GUEST"

def HasAuthority(request, s_name):  
  if request.user.is_authenticated():
    u_data = {'name':request.user.username,'email':request.user.email}
    try:
      service_data = ServiceCore.objects.get(name = s_name)
      if (service_data.email == u_data['email'] or u_data['name'] == 'SYSTEM'):
        return {'r':True,'m':'','s':service_data,'u':u_data}
      else:
        return {'r':False,'m':service_data.email+"["+u_data['name']+'|'+u_data['email']+"]"}
    except ServiceCore.DoesNotExist:
      return {'r':False,'m':"Service "+s_name+" does not exists"}
  else:
    return {'r':False,'m':"No login info found"}

def IsSystem(request):
  return (request.user.is_authenticated() and request.user.username == "SYSTEM")

class HistoryHelper:
  @staticmethod
  def Record(request,sname,acttype,para):
    today = date.today()
    now = datetime.now()
    hist = History() 
    hist.type = acttype
    hist.service = sname
    hist.who = GetUsr(request) 
    hist.para = para
    hist.save()
    try:
      service = ServiceCore.objects.get(name = sname)
      service.activity = service.activity + 1
      service.save()
    except ServiceCore.DoesNotExist:
      pass

  @staticmethod
  def GetHistory(sname,acttype,usrname,max):
    hs = History.objects.all().order_by("-id") 
    if(sname):
      hs = hs.filter(service = sname)
    if(acttype):
      hs = hs.filter(type = acttype)
    if(usrname):
      hs = hs.filter(who = usrname)
    if(max):
      hs = hs[:max]
    return hs

def Users(request):
    users = []
    if GetUsr(request) == "SYSTEM":
      users = User.objects.all()
    users_t = loader.get_template('usrs/users.html')
    fbdebug = "not detected"
    if request.session.has_key('login_debug'):
      fbdebug = request.session['login_debug']
    c = Context({"USERS":users,"FBDEBUG":fbdebug})
    return HttpResponse(users_t.render(c),mimetype = "text/html")

def RemoveUser(request,uname):
    command_error = {}
    try:
        user = User.objects.get(username = uname)
        User.delete(user) 
        return GeneralXMLResponse(request,command_error,"User " + uname + " was removed")
    except User.DoesNotExist:
        command_error['MISC'] = 'User ' + uname + ' Does Not Exist'
        return GeneralXMLResponse(request,command_error)
    users = User.objects.all()
    users_t = loader.get_template('usrs/users.html')
    c = Context({"USERS":users})
    return HttpResponse(users_t.render(c),mimetype = "text/html")
    
def Register(request):
    u_data = {}
    command_error = model_obj_builder(u_data,request.REQUEST,reg_handler)
    if command_error:
        register_t = loader.get_template('core/__register_fail.xml')
        c = Context({"ERROR":command_error})
        return HttpResponse(register_t.render(c),mimetype = "text/xml")
    else:
        try:
            user = User.objects.get(email=u_data['email'])
            command_error['ERROR'] = 'email conflict'
            return GeneralXMLResponse(request,command_error,'Email address already been used')
        except User.DoesNotExist:
            try:
              user = User.objects.create_user(u_data['name'],u_data['email'],u_data['password'])
              user.is_active = False
              user.save()
              t = loader.get_template('core/__register_succ.xml')
              c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST})
              tail = sha1(user.email)
              cmd.SendMail("(Auadult),Account registration",
                     """Welcome to www.auadult.com.au

Please use the following link to continue to register your account: http://%s/core/user/activate/%s/%s

Note: This link will be invalid after 2 hours."""
                     %(request.get_host(),user.username,tail.hexdigest()),
                     user.email) 
              return HttpResponse(t.render(c),mimetype = "text/xml")
            except ValidationError, e:
              command_error['ERROR'] = 'ValidationError';
              register_t = loader.get_template('core/__register_fail.xml')
              return GeneralXMLResponse(request,command_error,e)
            except IntegrityError, e:
              command_error['name'] = 'Name Already Exists';
              command_error['ERROR'] = 'Name Already Exists';
              return GeneralXMLResponse(request,command_error,e)

def ForgetPassword(request):
    u_data = {}
    command_error = model_obj_builder(u_data,request.REQUEST,forget_pw_handler)
    reset_t = loader.get_template('core/forgetpw.html')
    if command_error:
      MSG = '';
      if(command_error.has_key('email') and
         command_error['email'].startswith('Missing')):
        pass
      else:
        MSG = "you need to provide a valid email address"
      c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':MSG})
      return HttpResponse(reset_t.render(c),mimetype = "text/html")
    else:
      try:
        user = User.objects.get(email=u_data['email'])
        tail = sha1(user.email).hexdigest()
        request.session['resetpw'] = tail
        request.session['resetpw_email'] = user.email 
        cmd.SendMail("(Auadult), Reset password",
               """You have received this Email because you just made a request to reset password for your account of www.auadult.com.au.

Please use the following link to reset your password: http://%s/glue/changepw/?resetpw=%s

Note: This link will be invalid after 2 hours."""
                  %(request.get_host(),tail),
                  user.email) 
        c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':'Email has been delivered'})
        return HttpResponse(reset_t.render(c),mimetype = "text/html")
      except User.DoesNotExist:
        c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':'You need to provide the email address which you used to register your account'})
        return HttpResponse(reset_t.render(c),mimetype = "text/html")

def Reactivate(request):
    u_data = {}
    command_error = model_obj_builder(u_data,request.REQUEST,forget_pw_handler)
    reactivate_t = loader.get_template('core/reactivate.html')
    if command_error:
      MSG = '';
      if(command_error.has_key('email') and
         command_error['email'].startswith('Missing')):
        pass
      else:
        MSG = "you need to provide a valid email address"
      c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':MSG})
      return HttpResponse(reactivate_t.render(c),mimetype = "text/html")
    else:
      try:
        user = User.objects.get(email=u_data['email'])
        t = loader.get_template('core/__register_succ.xml')
        c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':'Email has been delivered'})
        tail = sha1(user.email)
        cmd.SendMail("(Auadult),accout activation",
                     """You have received this Email because you just made a request to activate your account of www.auadult.com.au.
Please use the following link to activate your account: http://%s/core/user/activate/%s/%s
Note: This link will be invalid after 2 hours."""
                     %(request.get_host(),user.username,tail.hexdigest()),
                     user.email) 
        return HttpResponse(reactivate_t.render(c),mimetype = "text/html")
      except User.DoesNotExist:
        c = Context({'CSS_ROOT':'default','REQUEST':request.REQUEST,'MSG':'You need to provide the email address which you used to register your account'})
        return HttpResponse(reset_t.render(c),mimetype = "text/html")

def ChangePWD(request):
    u_data = {}
    command_error = model_obj_builder(u_data,request.REQUEST,pwd_handler)
    if command_error:
        return GeneralXMLResponse(request,command_error,'Change Password Failed')
    elif request.user.is_authenticated():
        user = User.objects.get(username=request.user.username)
        user.set_password(u_data['password'])
        user.save()
        return GeneralXMLResponse(request,command_error,'Password Changed Successfully')
    else:
        command_error['ERROR'] = 'Login Required'
        return GeneralXMLResponse(request,command_error,'Login Required')

def Logout(request):
    logout(request)
    return HttpResponse('Logout');

def Activate(request,uname,code):
    command_error = {}
    try:
        user = User.objects.get(username = uname)
        user.is_active = True
        user.save()
        return redirect('/glue/login/?next=' + reverse("main.core.views.Search",args=()));
    except User.DoesNotExist:
        return redirect('/glue/register/');


@session_prefix      
def Login(request):
    command_error = {}
    u_data = {}
    command_error = model_obj_builder(u_data,request.REQUEST,login_handler)
    if command_error:
        return GeneralXMLResponse(request,command_error,'Login Failed: Missing Infomation')
    user = authenticate(username = u_data['name'],password = u_data['password'])
    if user is not None:
        if user.is_active:
            login(request,user)
        else:
            command_error['Validation'] = 'User has not been activated'
    else:
        command_error['Validation'] = 'Invalid Username Or Password'
    login_t = loader.get_template('core/__login.html')
    if command_error:
        return GeneralXMLResponse(request,command_error,'Login Failed')
    else:
        c = Context({'CSS_ROOT':'default','USER_NAME':u_data['name']})
        return HttpResponse(login_t.render(c),mimetype = "text/xml")
